/*
 * @Date: 2021-06-01 22:32:10
 * @LastEditors: kanoyami
 * @LastEditTime: 2021-06-01 23:23:58
 */
/*
 * @Date: 2021-01-30 18:33:55
 * @LastEditors: kanoyami
 * @LastEditTime: 2021-02-27 18:56:06
 */

const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const path = require('path')
const DB_DIR = path.join(process.cwd(), "db", "meme.json")
const adapter = new FileSync(DB_DIR)
const db = low(adapter)
db.defaults({ meme: [] })
    .write()


function MemeInsertOne(meme) {
    return db.get('meme')
        .push(meme)
        .write()
        .id
}

function MemeFindByCmd(cmd, room_id) {
    return db.get('meme')
        .filter({ cmd, room_id })
        .value()
}

function MemeFindByRoomid( room_id) {
    return db.get('meme')
        .filter({ room_id })
        .value()
}

function MemeDeleteByCmd(cmd,room_id){
    return db.get('meme')
    .remove(function(n) {
        return n.cmd === cmd && n.room_id === room_id;
      })
    .write();
}

module.exports = {
    MemeFindByCmd,
    MemeInsertOne,
    MemeFindByRoomid,
    MemeDeleteByCmd
}